﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using AIStudio.Wpf.DiagramDesigner.Geometrys;

namespace AIStudio.Wpf.DiagramDesigner
{
    public class ConnectorPoint : BindableBase
    {
        public ConnectorPoint()
        {
            ColorViewModel = new ColorViewModel()
            {
                LineColor = new ColorObject() { Color = Color.FromArgb(0xAA, 0x00, 0x00, 0x80) },
                FillColor = new ColorObject() { Color = Colors.Lavender },
            };
        }

        public ConnectorPoint(PointBase point) : this()
        {
            X = point.X;
            Y = point.Y;
        }

        public ConnectorPoint(double x, double y) : this()
        {
            X = x;
            Y = y;
        }

        /// <summary>
        /// 中间X
        /// </summary>
        private double _x;
        public double X
        {
            get
            {
                return _x;
            }
            set
            {
                if(SetProperty(ref _x, value))
                {
                    RaisePropertyChanged(nameof(Left));
                }
            }
        }

        /// <summary>
        /// 中间Y
        /// </summary>
        private double _y;
        public double Y
        {
            get
            {
                return _y;
            }
            set
            {
                if (SetProperty(ref _y, value))
                {
                    RaisePropertyChanged(nameof(Top));
                }
            }
        }

        /// <summary>
        /// 边界Left
        /// </summary>
        public double Left
        {
            get
            {
                return X - ConnectorWidth / 2;
            }
        }

        /// <summary>
        ///  边界Top
        /// </summary>
        public double Top
        {
            get
            {
                return Y - ConnectorHeight / 2;
            }
        }

        public PointBase Position
        {
            get
            {
                return new PointBase(Left, Top);
            }
        }
        public PointBase MiddlePosition => new PointBase(Left + ConnectorWidth / 2, Top + ConnectorHeight / 2);

        private double connectorWidth = 8;
        public double ConnectorWidth
        {
            get { return connectorWidth; }
            set { connectorWidth = value; }
        }

        private double connectorHeight = 8;
        public double ConnectorHeight
        {
            get { return connectorHeight; }
            set { connectorHeight = value; }
        }

        private IColorViewModel _colorViewModel;
        public IColorViewModel ColorViewModel
        {
            get
            {
                return _colorViewModel;
            }
            set
            {
                SetProperty(ref _colorViewModel, value);
            }
        }   

        public static ConnectorPoint operator -(ConnectorPoint a, ConnectorPoint b)
        {
            return new ConnectorPoint(a.X - b.X, a.Y - b.Y);
        }
        public static ConnectorPoint operator +(ConnectorPoint a, ConnectorPoint b)
        {
            return new ConnectorPoint(a.X + b.X, a.Y + b.Y);
        }

        public static implicit operator ConnectorPoint(PointBase point)
        {
            return new ConnectorPoint(point);
        }

        public static implicit operator PointBase(ConnectorPoint pointInfoBase)
        {
            return new PointBase(pointInfoBase.X, pointInfoBase.Y);
        }

        public static List<ConnectorPoint> ToList(List<PointBase> lst)
        {
            return lst.Select(p => (ConnectorPoint)p).ToList();
        }

        public override string ToString() => $"ConnectorPoint(x={X}, y={Y})";
    }
}
